package com.zm.demo.core.leetcode;

import static java.lang.System.out;

/**
 * @author zoum
 * @create 2020/1/9 15:16
 */
public class MinPathSum {

    Integer min = Integer.MAX_VALUE;

    public int minPathSum(int[][] grid) {

        if (grid == null || grid.length == 0 || grid[0] == null || grid[0].length == 0){
            return 0;
        }

        int iLength = grid.length;
        int jLength = grid[0].length;

        int[][]dp = new int[iLength][jLength];
        dp[0][0] = grid[0][0];
        for (int i = 1; i< iLength; i++){
            dp[i][0] = dp[i-1][0] + grid[i][0];
        }

        for (int i = 1; i< jLength; i++){
            dp[0][i] = dp[0][i-1] + grid[0][i];
        }

        for (int i = 1; i < iLength ; i++) {
            for (int j = 1; j < jLength ; j++) {
                dp[i][j] = grid[i][j] + Math.min(dp[i][j-1],dp[i-1][j]);
            }
        }
        return dp[iLength-1][jLength-1];
    }





    public static void main(String[]args){
        int a[][] = {{1,3,1},{1,5,1},{4,2,1}};
        out.println(new MinPathSum().minPathSum(a));
    }
}
